package com.ycrlkj.android.modulescore.main.xpop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.easyadapter.EasyAdapter;
import com.lxj.easyadapter.MultiItemTypeAdapter;
import com.lxj.easyadapter.ViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.lxj.xpopup.widget.VerticalRecyclerView;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.XxStationApiService;
import com.ycrlkj.android.modulescore.main.entity.XxStation;
import com.ycrlkj.android.modulescore.main.entity.Query.XxStationQuery;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class PopupBottomSelectXxStation extends BottomPopupView {

    @Inject
    XxStationApiService xxStationApiService;

    private VerticalRecyclerView recyclerView;

    private EasyAdapter<XxStation> commonAdapter;

    private OnSelectedListener onSelectedListener;

    public PopupBottomSelectXxStation(@NonNull Context context, OnSelectedListener onSelectedListener) {
        super(context);
        this.setOnSelectedListener(onSelectedListener);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_bottom_freight_order_select;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(R.id.recyclerView);

        findViewById(R.id.btn_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        getAll();
    }

    @SuppressLint("CheckResult")
    public void getAll() {
        XxStationQuery xxStationQuery = new XxStationQuery();

        xxStationApiService.getAll(xxStationQuery)
                .compose(ResponseTransformer.obtain())
                .subscribe(xxStations -> {
                            commonAdapter = new EasyAdapter<XxStation>(xxStations, R.layout.item_pop_select_xx_station) {
                                @Override
                                protected void bind(@NonNull ViewHolder viewHolder, XxStation xxStations, int position) {

                                    viewHolder.setText(R.id.tv_name, xxStations.getName())
                                            .setText(R.id.tv_locationDistance, xxStations.getLocationDistance() == null ? "" : String.valueOf(xxStations.getLocationDistance()))
                                            .setText(R.id.tv_today, xxStations.getToday())
                                            .setText(R.id.tv_todayAppointment, xxStations.getTodayAppointment() == null ? "" : String.valueOf(xxStations.getTodayAppointment()))
                                            .setText(R.id.tv_todayCheck, xxStations.getTodayCheck() == null ? "" : String.valueOf(xxStations.getTodayCheck()))
                                            .setText(R.id.tv_tomorrow, xxStations.getTomorrow())
                                            .setText(R.id.tv_tomorrowAppointment, xxStations.getTomorrowAppointment() == null ? "" : String.valueOf(xxStations.getTomorrowAppointment()))
                                            .setText(R.id.tv_tomorrowCheck, xxStations.getTomorrowCheck() == null ? "" : String.valueOf(xxStations.getTomorrowCheck()))
                                            .setText(R.id.tv_thirdDate, xxStations.getThirdDate())
                                            .setText(R.id.tv_thirdDateAppointment, xxStations.getThirdDateAppointment() == null ? "" : String.valueOf(xxStations.getThirdDateAppointment()))
                                            .setText(R.id.tv_thirdDateCheck, xxStations.getThirdDateCheck() == null ? "" : String.valueOf(xxStations.getThirdDateCheck()));

                                }
                            };

                            //item点击监听
                            commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener() {
                                @Override
                                public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                                    //不要直接 dismiss(); 会导致消失动画未执行完就跳转界面
                                    //使用 dismissWith() 等消失动画执行完毕
                                    dismissWith(new Runnable() {
                                        @Override
                                        public void run() {
                                            onSelectedListener.selected(xxStations.get(position));
                                        }
                                    });
                                }
                            });
                            recyclerView.setAdapter(commonAdapter);
                        },
                        new ErrorConsumer() {
                            @Override
                            protected void error(ApiException e) {
                                ToastUtils.showLong(e.getMessage());
                            }
                        });

    }


    public interface OnSelectedListener {
        void selected(XxStation xxStation);
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }

}